#include "mainview.h"
#include "libs/extension/extensionview.h"
#include "modules/analyser/analyser.h"
#include "modules/comparer/comparer.h"
#include "modules/localdictionary/localdictionaryeditor.h"
#include "modules/recorder/liverecorder.h"
#include "modules/screencapture/screencapture.h"
#include "settings/settingsview.h"
#include <QButtonGroup>
#include <QFrame>
#include <QIcon>
#include <QMessageBox>
#include <QPushButton>
#include <QStackedWidget>
#include <QVBoxLayout>

MainView::MainView(QWidget* parent) : QWidget { parent }
{
    setupUi();
}

MainView::~MainView()
{
    delete recorder_;
    delete analyser_;
    delete screen_capture_;
    delete comparer_;
    delete local_dict_editor_;
}

void MainView::requestStop(const yc::ResolveCallback& resolve, const yc::RejectCallback& reject)
{
    if (recorder_->isRecording() || (screen_capture_ && screen_capture_->isRecording())) {
        auto ret =
                QMessageBox::question(this, tr("Confirm"),
                                      tr("Recording is in progress. Are you sure to exit? Exit and "
                                         "the recorded content will not be saved."),
                                      QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);
        if (ret != QMessageBox::Ok) {
            if (reject) {
                reject();
            }
            return;
        }
    }

    if (resolve) {
        resolve();
    }
}

void MainView::stop() { }

void MainView::setupUi()
{
    QHBoxLayout* hl = new QHBoxLayout(this);
    hl->setContentsMargins(0, 0, 0, 0);
    hl->setSpacing(0);

    QFrame* side_bar = new QFrame(this);
    side_bar->setObjectName("side_bar");
    hl->addWidget(side_bar);

    QVBoxLayout* button_layout = new QVBoxLayout(side_bar);
    button_layout->setContentsMargins(3, 4, 3, 4);
    button_layout->setSpacing(2);

    QPushButton* recorder_button = new QPushButton(tr("Recorder"), side_bar);
    recorder_button->setObjectName("recorder");
    recorder_button->setCheckable(true);
    recorder_button->setChecked(true);
    recorder_button->setIconSize(QSize(20, 16));
    recorder_button->setIcon(QIcon(":/images/presenter.png"));
    button_layout->addWidget(recorder_button);

    QPushButton* capture_button = new QPushButton(tr("ScreenCapture"), side_bar);
    capture_button->setObjectName("screen_capture");
    capture_button->setCheckable(true);
    capture_button->setIconSize(QSize(20, 16));
    capture_button->setIcon(QIcon(":/images/screencapture.png"));
    button_layout->addWidget(capture_button);

    QPushButton* analyser_button = new QPushButton(tr("Analyser"), side_bar);
    analyser_button->setObjectName("analyser");
    analyser_button->setCheckable(true);
    analyser_button->setIconSize(QSize(20, 16));
    analyser_button->setIcon(QIcon(":/images/analyser.png"));
    button_layout->addWidget(analyser_button);

    QPushButton* comparer_button = new QPushButton(tr("Comparer"), side_bar);
    comparer_button->setObjectName("comparer");
    comparer_button->setCheckable(true);
    comparer_button->hide();
    button_layout->addWidget(comparer_button);

    QPushButton* local_dict_button = new QPushButton(tr("Local Dictionary"), side_bar);
    local_dict_button->setObjectName("local_dict");
    local_dict_button->setCheckable(true);
    local_dict_button->hide();
    button_layout->addWidget(local_dict_button);

    QPushButton* settings_button = new QPushButton(tr("Settings"), side_bar);
    settings_button->setObjectName("settings");
    settings_button->setCheckable(true);
    settings_button->setIconSize(QSize(20, 16));
    settings_button->setIcon(QIcon(":/images/settings.png"));
    button_layout->addStretch(1);
    button_layout->addWidget(settings_button);

    QVBoxLayout* vl = new QVBoxLayout;
    vl->setContentsMargins(0, 0, 0, 0);
    vl->setSpacing(0);
    hl->addLayout(vl);

    stacked_widget_ = new QStackedWidget(this);
    stacked_widget_->setObjectName("content_panel");
    stacked_widget_->setContentsMargins(0, 0, 0, 0);
    vl->addWidget(stacked_widget_, 1);

    recorder_ = new LiveRecorder(nullptr, this);
    stacked_widget_->addWidget(recorder_->view());

    QButtonGroup* group = new QButtonGroup(this);
    group->setExclusive(true);
    group->addButton(recorder_button, 0);
    group->addButton(analyser_button, 1);
    group->addButton(capture_button, 2);
    group->addButton(comparer_button, 3);
    group->addButton(local_dict_button, 4);
    group->addButton(settings_button, 5);

    connect(group, &QButtonGroup::idClicked, this, [this](int id) {
        switch (id) {
        case 0:
            stacked_widget_->setCurrentIndex(0);
            break;
        case 1:
            if (!analyser_) {
                analyser_ = new Analyser(nullptr, this);
                stacked_widget_->addWidget(analyser_->view());
            }
            stacked_widget_->setCurrentWidget(analyser_->view());
            break;
        case 2:
            if (!screen_capture_) {
                screen_capture_ = new ScreenCapture(nullptr, this);
                stacked_widget_->addWidget(screen_capture_->view());
            }
            stacked_widget_->setCurrentWidget(screen_capture_->view());
            break;
        case 3:
            if (!comparer_) {
                comparer_ = new Comparer(nullptr, this);
                stacked_widget_->addWidget(comparer_->view());
            }
            stacked_widget_->setCurrentWidget(comparer_->view());
            break;
        case 4:
            if (!local_dict_editor_) {
                local_dict_editor_ = new LocalDictionaryEditor(nullptr, this);
                stacked_widget_->addWidget(local_dict_editor_->view());
            }
            stacked_widget_->setCurrentWidget(local_dict_editor_->view());
            break;
        case 5:
            if (!settings_view_) {
                settings_view_ = new SettingsView(this);
                stacked_widget_->addWidget(settings_view_);
            }
            stacked_widget_->setCurrentWidget(settings_view_);
            break;
        default:
            break;
        }
    });
}
